package com.yqy.wordvideodemo.ui

import android.app.Activity
import android.content.Intent
import android.content.pm.ActivityInfo
import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import com.shuyu.gsyvideoplayer.GSYVideoManager
import com.shuyu.gsyvideoplayer.utils.OrientationUtils
import com.yqy.wordvideodemo.R
import kotlinx.android.synthetic.main.activity_simple_play.*

class VideoShowActivity : AppCompatActivity() {


    companion object {
        private const val KEY_VIDEO_URL = "videoUrl"

        fun startVideoActivity(act: Activity, url: String?) {
            val intent = Intent(act, VideoShowActivity::class.java)
            intent.putExtra(KEY_VIDEO_URL, url)
            act.startActivity(intent)
        }
    }

    private lateinit var orientationUtils: OrientationUtils

    private fun getVideoUrl(): String? {
        return intent.getStringExtra(KEY_VIDEO_URL)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_simple_play)
        init()
    }

    private fun init() {
        video_player.setUp(getVideoUrl(), true, "标题")

        //增加封面
//        val imageView = ImageView(this)
//        imageView.scaleType = ImageView.ScaleType.CENTER_CROP
//        imageView.setImageResource(R.mipmap.xxx1)
//        video_player.setThumbImageView(imageView)
        //增加title
        video_player.titleTextView.visibility = View.GONE
        //设置返回键
        video_player.backButton.visibility = View.VISIBLE
        //设置旋转
        orientationUtils = OrientationUtils(this, video_player)
        //设置全屏按键功能,这是使用的是选择屏幕，而不是全屏
        video_player.fullscreenButton.setOnClickListener { orientationUtils.resolveByClick() }
        //是否可以滑动调整
        video_player.setIsTouchWiget(true)
        //设置返回按键功能
        video_player.backButton.setOnClickListener { onBackPressed() }
        video_player.startPlayLogic()
    }

    override fun onPause() {
        super.onPause()
        video_player.onVideoPause()
    }

    override fun onResume() {
        super.onResume()
        video_player.onVideoResume()
    }

    override fun onDestroy() {
        super.onDestroy()
        GSYVideoManager.releaseAllVideos()
        orientationUtils.releaseListener()
    }

    override fun onBackPressed() {
        //先返回正常状态
        if (orientationUtils.screenType == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
            video_player.fullscreenButton.performClick()
            return
        }
        //释放所有
        video_player.setVideoAllCallBack(null)
        super.onBackPressed()
    }
}